// 本文件对LpPacket的相关功能进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/16 13:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const benchmark = require('benchmark')
const LpPacket = require("./LpPacket");
const {createPayLoad} = require("../common/createPayload");
const {createBlockByBuffer} = require("../encoding/Block");
const {Encoder, MaxPacketSize} = require("../encoding/Encoder")

const payload = createPayLoad(0), payload1K = createPayLoad(1), payload8K = createPayLoad(8)
// 测试WireEncode
function createLpPacket(payload) {
    let lpPacket = new LpPacket()
    lpPacket.Payload.setValue(payload)
    lpPacket.LpPacketHeader.LpPacketFragmentId.setId(1)
    lpPacket.LpPacketHeader.LpPacketFragmentNum.setFragmentNum(1)
    lpPacket.LpPacketHeader.LpPacketFragmentSeq.setFragmentSeq(1)
    return lpPacket
}

let lpPacket = createLpPacket(payload), lpPacket1K = createLpPacket(payload1K), lpPacket8K = createLpPacket(payload8K)

const suite1 = new benchmark.Suite()
suite1.add('wireEncode 1Byte', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    lpPacket.wireEncode(enc)
}).add('wireEncode 1K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    lpPacket1K.wireEncode(enc)
}).add('wireEncode 8K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    lpPacket8K.wireEncode(enc)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireEncode finished')
}).run()

let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
lpPacket.wireEncode(enc)
let buf = enc.getBuffer()
let lpPacket2 = new LpPacket()
let block = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
lpPacket1K.wireEncode(enc)
buf = enc.getBuffer()
let lpPacket2_1K = new LpPacket()
let block1K = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
lpPacket8K.wireEncode(enc)
buf = enc.getBuffer()
let lpPacket2_8K = new LpPacket()
let block8K = createBlockByBuffer(buf, false)
const suite3 = new benchmark.Suite()
suite3.add('wireDecode 1Byte', ()=>{
    lpPacket2.wireDecode(block)
}).add('wireDecode 1K', ()=>{
    lpPacket2_1K.wireDecode(block)
}).add('wireDecode 8K', ()=>{
    lpPacket2_8K.wireDecode(block)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireDecode finished')
}).run()
